package com.ds.api.biz;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ds.api.dao.UserKeyMapper;
import com.ds.api.model.UserKey;

@Service
public class UserServiceBiz {
    @Autowired
    private UserKeyMapper mapper;
    public String insertUserKey(Integer userId){
        String key=getUuid();
        UserKey user=new UserKey();
        user.setFkUserBaseinfoId(userId);
        List<UserKey> list=mapper.query(user);
        if(list.size()>0){
            UserKey keyBean = list.get(0);
            keyBean.setUidKey(key);
            keyBean.setIsActive("0");
            keyBean.setUpdateTime(new Date());
            mapper.updateByPrimaryKeySelective(keyBean);
        }else{
            UserKey keyBean = new UserKey();
            keyBean.setIsActive("0");
            keyBean.setUidKey(key);
            keyBean.setCreateTime(new Date());
            keyBean.setFkUserBaseinfoId(userId);
            keyBean.setUpdateTime(new Date());
            mapper.insert(keyBean);
        }
        return key;
    }
    
    
    public static String getUuid() {
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        String uuidStr = str.substring(0, 8) + str.substring(9, 13)
                + str.substring(14, 18) + str.substring(19, 23)
                + str.substring(24);
        return uuidStr;
    }
}
